#include <QDebug>
#include <QDateTime>

#include "housecreatewidget.h"
#include "ui_housecreatewidget.h"
#include "mainwidget.h"


HouseCreateWidget::HouseCreateWidget(TcpSession *session,MainWidget* mainWidget, QWidget *parent) :
    QWidget(parent),ui(new Ui::HouseCreateWidget),mainWidget(mainWidget),session(session)
{
    ui->setupUi(this);
    //
    QObject::connect(session, &TcpSession::house_add_signal, this, &HouseCreateWidget::on_house_add_signal);
    QObject::connect(session, &TcpSession::logout_call_signal, this, &HouseCreateWidget::on_logout_call_signal);
}

void HouseCreateWidget::show(QString & username, QString & nickname)
{
    this->username=username;
    this->nickname=nickname;
    //界面
    ui->lineEdit_housename->setText("["+nickname+"]的直播间");
    ui->slider_limit->setValue(10);
    ui->label_limit->setText("10");

    QDateTime time1=QDateTime::currentDateTime();
    ui->dateTimeEdit_start->setDateTime(time1);
    QDateTime time2=time1.addSecs(6*60*60);
    ui->dateTimeEdit_end->setDateTime(time2);
    //网络
    this->QWidget::show();
    //

}

void HouseCreateWidget::on_pushButton_create_clicked()
{
    QString housename=ui->lineEdit_housename->text();
    int limit=ui->slider_limit->value();
    QString starttime=ui->dateTimeEdit_start->text();
    QString endtime=ui->dateTimeEdit_end->text();
    //
    Pack pack;
    pack.setType(HOUSE_ADD);
    pack.setFrame(CALL);
    pack.append(username);
    pack.append(housename);
    pack.append(username);
    pack.append(QString::number(limit));
    pack.append(starttime);
    pack.append(endtime);
    session->send((char*)&pack, pack.getPacksize());
}

void HouseCreateWidget::on_pushButton_cancel_clicked()
{
    this->hide();
    mainWidget->show();
}

void HouseCreateWidget::on_slider_limit_valueChanged(int value)
{
    ui->label_limit->setText(QString::number(value));
}

void HouseCreateWidget::on_house_add_signal(bool res, const QString &msg)
{
    if(res==true){
        QMessageBox::information(this, "房间创建", "房间创建成功");
        on_pushButton_cancel_clicked();
        return;
    }
    QMessageBox::critical(this, "房间创建失败", msg);
}

void HouseCreateWidget::on_logout_call_signal(const QString & msg)
{
    QMessageBox::critical(this,"登出提示", msg);
    exit(EXIT_SUCCESS);
}

